package com.yanqu.road.server.cmd.activity.summerquiz;

import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summerquiz.SummerQuizConfig;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerQuizProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.manger.activity.summerquiz.SummerQuizActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_SUMMER_QUIZ_USER_GET, desc = "玩家数据获取")
@CmdApi(reqMsg = SummerQuizProto.SummerQuizUserDataGetReqMsg.class, respMsg = SummerQuizProto.SummerQuizUserDataGetRespMsg.class)
public class SummerQuizUserDataGetCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        SummerQuizConfig config = SummerQuizActivityMgr.getConfig();
        // 没有活动
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        if(!SystemOpenMgr.systemOpen(player, eSystemId.SummerQuizActivity.getValue())){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        SummerGamesConfig gamesConfig = SummerGamesActivityMgr.getSummerGamesConfig();
        // 没有活动
        if (gamesConfig == null || !ActivityMgr.activityInShowTime(gamesConfig.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        SummerQuizProto.CrossSummerQuizUserDataGetReqMsg.Builder crossReq = SummerQuizProto.CrossSummerQuizUserDataGetReqMsg.newBuilder();
        crossReq.setActivityId(config.getActivityInfo().getActivityId())
                .setGameActivityId(gamesConfig.getActivityId());
        player.sendPacket(Protocol.C_SUMMER_QUIZ_USER_GET, crossReq);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_SUMMER_QUIZ_USER_GET, SummerQuizProto.SummerQuizUserDataGetRespMsg.newBuilder().setRet(ret));
        }
    }
}
